package com.graduation.fq.teacher.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.TextView;
import android.widget.Toast;

import com.google.common.base.Strings;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by jifang
 * Time 2016/5/27 9:55.
 */
public class CommonUtils {

    public static void Toast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static String getString(TextView text) {
        return text.getText().toString().trim();
    }

    public static boolean isNotNullOrEmpty(String... strings) {
        if (strings == null) {
            return false;
        } else {
            for (String str : strings) {
                if (Strings.isNullOrEmpty(str)) {
                    return false;
                }
            }
        }

        return true;
    }

    public static String getUserId(Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString("user_id", "");
    }

    public static void saveUserId(Context context, String id) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        preferences.edit().putString("user_id", id).apply();
    }

    public static String encode(String string){
        try {
            return URLEncoder.encode(string, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
